package cn.jbit.ams.mainperson.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import cn.jbit.ams.mainperson.biz.MainpersonService;
import cn.jbit.ams.order.biz.OrderService;
import cn.jbit.ams.user.biz.UserService;
import cn.jbit.ams.user.pojo.Mainperson;
import cn.jbit.ams.user.pojo.User;
import net.sf.json.JSONArray;

@Controller
public class MainpersonController {
	@Autowired
	private MainpersonService mainService;
	
	@Autowired
	private OrderService orderService;

	/**
	 * 处理维修人员 查询 上一页信息  下一页信息
	 * @param pageNum
	 * @param mName
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPageMainperson")
	public String getPageMainperson(Integer pageNum,String mName,Model model) {
		//查询维修人员信息  包括模糊查询  分页查询
		Page<Mainperson> mainPage=mainService.findMainByName(pageNum, mName);
		model.addAttribute("mainPage", mainPage);
		String userPageList=JSON.toJSONString(mainPage);
		return userPageList;
	}
	/**
	 * 获取指定的维修人员信息
	 * @param pageNum
	 * @param mName
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getMainOne")
	public String getMainOne(Integer pageNum,Integer mId,Model model) {
		//点击修改  显示数据到文本框的对象  
		Mainperson mainperson=mainService.fondMainOne(mId);
		String mainOne=JSON.toJSONString(mainperson);
		//返回维修JSON到前端
		return mainOne;
	}
	@ResponseBody
	@RequestMapping("updateMainperson")
	public String updateMainperson(Mainperson mainperson,Integer pageNum,String mainName,Model model) {
		mainService.updateMainperson(mainperson);
		Page<Mainperson> mainPage=mainService.findMainByName(pageNum, mainName);
		model.addAttribute("mainPage", mainPage);
		String userPageList=JSON.toJSONString(mainPage);
		return userPageList;
	}
	
	@ResponseBody
	@RequestMapping("delMainAndOrder")
	public String updateMainperson(Integer pageNum,String mName,Integer mId,Model model) {
		int result=orderService.deleteOrder(mId);
		if(result==1) {
			mainService.deleteMainperson(mId);
		}
		Page<Mainperson> mainPage=mainService.findMainByName(pageNum, mName);
		model.addAttribute("mainPage", mainPage);
		String userPageList=JSON.toJSONString(mainPage);
		return userPageList;
	}
	
	@ResponseBody
	@RequestMapping("/ajax_01")
	public JSONArray  ajax(String index) {
		System.out.println(index);
		JSONArray date = JSONArray.fromObject(mainService.ajax_01(index));   
		System.out.println(date);
		return date;
	}
	@ResponseBody
	@RequestMapping("/abc")
	public JSONArray  abc(String name) {
		System.out.println(name);
		JSONArray date = JSONArray.fromObject(mainService.abc(name));   
		System.out.println(date);
		return date;
	}
}
